package com.taotao.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taotao.base.BaseService;
import com.taotao.entity.MayiktConfigEntity;
import com.taotao.entity.MayiktConfigInfoEntity;
import com.taotao.mapper.MayiktConfigInfoMapper;
import com.taotao.mapper.MayiktConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangjin
 * @title: ConfigService
 * @projectName sw-springcloud
 * @description:
 * @date 2021/11/9 21:21
 */
@RestController
public class ConfigService extends BaseService {
    /**
     * 1,定义一个接口出来，获取配置文件
     */
    @Autowired
    private MayiktConfigMapper mayiktConfigMapper;

    @Autowired
    private MayiktConfigInfoMapper mayiktConfigInfoMapper;

    /**
     * md5 比对配置文件内容是否发生变化
     *
     * @param name
     * @return
     */
    @GetMapping("/md5Comparison")
    public ResponseEntity md5Comparison(String name, String md5) {
        if (StringUtils.isEmpty(name)) {
            return setResultFail("error :name is null ");
        }
        if (StringUtils.isEmpty(md5)) {
            return setResultFail("error :md5 is null ");
        }
        MayiktConfigEntity mayiktConfigEntity = mayiktConfigMapper.getByConfigName(name);
        if (mayiktConfigEntity == null) {
            return setResultFail("The profile content is not configured on the server ");
        }
        if (mayiktConfigEntity.getMd5().equals(md5)) {
            return setResultOk("ok");
        }
        JSONObject data = resultData(mayiktConfigEntity);
        return data != null ? setResultOk("配置文件内容发生了变化", data, 201) :
                setResultFail("The profile content is not configured on the server ");

    }

    /**
     * 根据服务名称读取配置文件
     *
     * @param name
     * @return
     */
    @GetMapping("/getConfig")
    public ResponseEntity getConfig(String name) {
        if (StringUtils.isEmpty(name)) {
            return setResultFail("error :name is null ");
        }
        // 根据该服务名称查找db 配置文件内容
        MayiktConfigEntity mayiktConfigEntity = mayiktConfigMapper.getByConfigName(name);
        if (mayiktConfigEntity == null) {
            return setResultFail("The profile content is not configured on the server ");
        }
        JSONObject data = resultData(mayiktConfigEntity);
        return data != null ? setResultOk("ok", data) : setResultFail("The profile content is not configured on the server ");
    }

    private JSONObject resultData(MayiktConfigEntity mayiktConfigEntity) {
        // 根据配置文件查找详细配置文件内容
        Long confId = mayiktConfigEntity.getId();
        List<MayiktConfigInfoEntity> mayiktConfigInfoList = mayiktConfigInfoMapper.getByConfigId(confId);
        if (mayiktConfigInfoList == null || mayiktConfigInfoList.size() == 0) {
            return null;
        }
        JSONObject data = new JSONObject();
        data.put("name", mayiktConfigEntity.getName());
        data.put("configId", confId);
        JSONArray jsonArray = new JSONArray();
        mayiktConfigInfoList.forEach((t) -> {
            JSONObject configInfo = new JSONObject();
            configInfo.put("key", t.getKey());
            configInfo.put("value", t.getValue());
            jsonArray.add(configInfo);
        });
        data.put("config", jsonArray);
        data.put("md5", mayiktConfigEntity.getMd5());
        return data;
    }
}
